package com.haixiaoke.saas.manage.department.controller;

import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.domain.entity.SysMenu;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.utils.StringUtils;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.department.domain.Tenant;
import com.haixiaoke.saas.department.service.IBizTenantMenuService;
import com.haixiaoke.saas.department.service.ITenantService;
import com.haixiaoke.saas.system.BaseController;
import com.haixiaoke.saas.system.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 机构信息Controller
 *
 * @author xiaolong
 * @date 2023-12-26
 */
@RestController
@RequestMapping("/manage/department/department")
public class ManageTenantController extends BaseController {
    @Autowired
    private ITenantService departmentService;
    @Autowired
    private IBizTenantMenuService iBizTenantMenuService;
    @Autowired
    private ISysMenuService menuService;

    /**
     * 查询机构信息列表
     */
    @PreAuthorize("@ss.hasPermi('manage:department:page')")
    @GetMapping("/page")
    public TableDataInfo page(Tenant department) {
        startPage();
        List<Tenant> list = departmentService.selectDepartmentList(department);
        return getDataTable(list);
    }

    /**
     * 查询机构信息列表
     */
    @GetMapping("/list")
    public AjaxResult list(Tenant department) {
        List<Tenant> list = departmentService.selectDepartmentList(department);
        return AjaxResult.success(list);
    }

    /**
     * 导出机构信息列表
     */
    @PreAuthorize("@ss.hasPermi('manage:department:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Tenant department) {
        List<Tenant> list = departmentService.selectDepartmentList(department);
        ExcelUtil<Tenant> util = new ExcelUtil<Tenant>(Tenant.class);
        util.exportExcel(response, list, "机构信息数据");
    }

    /**
     * 获取机构信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:department:query')")
    @GetMapping("/{tenantId}")
    public AjaxResult getInfo(@PathVariable("tenantId") String tenantId) {
        return success(departmentService.selectDepByDepId(tenantId));
    }

    /**
     * 新增机构信息
     */
    @PreAuthorize("@ss.hasPermi('manage:department:add')")
    @PostMapping
    public AjaxResult add(@RequestBody Tenant department) {
        department.setCreateBy(getUsername());
        return toAjax(departmentService.insertDepartment(department));
    }

    /**
     * 修改机构信息
     */
    @PreAuthorize("@ss.hasPermi('manage:department:edit')")
    @PutMapping
    @TenantScope
    public AjaxResult edit(@RequestBody Tenant department) {
        // if (departmentService.checkPopulation(department.getTenantId())){
        //     return error("该企业的用户数已超过当前企业最大人数");
        // }
        department.setUpdateBy(getUsername());
        return toAjax(departmentService.manageUpdateDepartment(department));
    }

    /**
     * 删除机构信息
     */
    @PreAuthorize("@ss.hasPermi('manage:department:remove')")
    @DeleteMapping("/{tenantIds}")
    public AjaxResult remove(@PathVariable String[] tenantIds) {
        if (tenantIds == null || tenantIds.length == 0) {
            return warn("请输入数据");
        }
        return toAjax(departmentService.deleteDepartmentByDepIds(tenantIds));
    }

    /**
     * 设置机构权限菜单
     */
    @PreAuthorize("@ss.hasPermi('system:menu:edit')")
    @PutMapping("/editMenuByDep")
    public AjaxResult editMenuByDep(@RequestBody Tenant department) {
        if (StringUtils.isBlank(department.getTenantId()) || StringUtils.isEmpty(department.getMenuIds())) {
            return AjaxResult.error("菜单数据缺失");
        }
        return toAjax(departmentService.editMenuByDep(department));
    }

    /**
     * 查询该机构的菜单权限与所有菜单--树状结构
     */
    @PreAuthorize("@ss.hasPermi('system:menu:list')")
    @GetMapping("/depMenuTreeselect/{tenantId}")
    public AjaxResult depMenuTreeselect(@PathVariable("tenantId") String tenantId) {
        List<SysMenu> sysMenus = menuService.selectMenuList(1L);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("menuIdsByDep", iBizTenantMenuService.selectMenuIdsByDepId(tenantId));
        ajax.put("menus", menuService.buildMenuTreeSelect(sysMenus));
        return ajax;
    }
}
